<template>
  <div class="banner">
    <el-carousel :interval="5000" type="card" height="548px">
      <el-carousel-item v-for="(item, index) in bannerList" :key="index">
        <div class="banner-bg" :style="'background-image:url(' + item.image + ')'"></div>
        <div class="banner-wrap"></div>
        <div class="banner-item">
          <div class="right-box">
            <div class="name">{{item.name}}</div>
            <div class="writer">{{item.writer}} 著</div>
            <div class="tag">
              <el-tag effect="plain">{{item.is_over}}</el-tag>
              <el-tag effect="plain">{{item.type}}</el-tag>
            </div>
            <div class="intro">{{item.intro}}</div>
            <div class="button">
              <div class="open">
                <el-button round @click="handleToChapter(item.id)">翻开看看</el-button>
              </div>
              <div class="collection">
                <el-button
                  :icon="item.isCollect?'el-icon-star-on':'el-icon-star-off'"
                  circle
                  @click="handleToAddBookShelf(item.isCollect,item.book_shelf_id,item.id)"
                ></el-button>
              </div>
            </div>
          </div>
          <div class="letf-box">
            <el-image
              style="height:419px;width:318px;border-radius:12px;"
              :src="item.image"
              fit="fill"
            >
              <div slot="error">
                <img src="http://foglake.sanye666.top/images/default-covor.jpg" alt />
              </div>
            </el-image>
          </div>
        </div>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<script>
import { getIndexList } from "@/api/homepage";
import { bookShelfList, addBookShelf, delBookShelf } from "@/api/shelf";
export default {
  name: "banner",
  data() {
    return {
      bannerList: [],
      getListLoad: false,
      shelfList: [],
    };
  },
  created() {
    this.init();
  },
  methods: {
    async init() {
      await this.getCollect();
      this.getList();
    },
    getList() {
      this.getListLoad = true;
      getIndexList()
        .then((res) => {
          let bookList = res.data.bookList.data;
          bookList.forEach((item) => {
            if (this.setCollect(item.id) == 0) {
              item.isCollect = false;
            } else {
              item.isCollect = true;
              item.book_shelf_id = this.setCollect(item.id);
            }
          });
          this.bannerList = bookList.slice(0, 5);
          this.getListLoad = false;
        })
        .catch((error) => {
          console.log(error);
          this.getListLoad = false;
        });
    },
    getCollect() {
      return new Promise((resolve, reject) => {
        if (this.$store.state.user.loginStatus) {
          const headers = {
            Authorization: `Bearer ${this.$store.state.user.token}`,
          };
          bookShelfList(headers).then((res) => {
            if (res.code == 200) {
              this.shelfList = res.data.bookList;
              resolve("ok");
            } else {
              reject();
            }
          });
        } else {
          resolve("ok");
        }
      });
    },
    setCollect(id) {
      let flag = 0;
      this.shelfList.forEach((item) => {
        if (item.book_id == id) {
          flag = item.book_shelf_id;
        }
      });
      return flag;
    },
    handleToChapter(id) {
      this.$router.push({
        path: "/chapter",
        query: { id },
      });
    },
    async handleToAddBookShelf(isCollect, book_shelf_id, id) {
      if (this.$store.state.user.loginStatus) {
        const headers = {
          Authorization: `Bearer ${this.$store.state.user.token}`,
        };
        if (isCollect) {
          if (book_shelf_id == 0) {
            await this.getBookShelfList();
          }
          delBookShelf({ bookShelfId: book_shelf_id }, headers).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "移除书架成功",
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          addBookShelf({ bookId: id }, headers).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.init();
            }
          });
        }
        this.bannerList.forEach(item =>{
          if(item.id == id){
            item.isCollect = !item.isCollect;
          }
        })
      } else {
        this.$message.error("您还未登录!");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.banner {
  margin-bottom: 88px;
  padding-top: 20px;
  .banner-bg {
    height: 100%;
    width: 100%;
    background-repeat: no-repeat;
    background-size: 1200px 548px;
    filter: blur(20px);
  }
  .banner-wrap {
    height: 100%;
    width: 100%;
    position: relative;
    bottom: 548px;
    left: 0;
    background: #00266b;
    opacity: 0.3;
  }
  .banner-item {
    height: 100%;
    width: 100%;
    position: relative;
    bottom: 1096px;
    left: 0;
    padding: 46px 46px 45px 60px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    .right-box {
      width: 670px;
      text-align: left;
      position: relative;
      .name {
        font-size: 36px;
        font-family: MicrosoftYaHei-Bold;
        font-weight: bold;
        color: rgba(255, 255, 255, 1);
        margin-bottom: 14px;
      }
      .writer {
        font-size: 16px;
        font-family: MicrosoftYaHeiSemibold;
        font-weight: 600;
        color: rgba(255, 255, 255, 1);
        line-height: 24px;
        opacity: 0.85;
        margin-bottom: 16px;
      }
      .tag {
        border-top: 1px solid #ffffff;
        border-bottom: 1px solid #ffffff;
        margin-bottom: 24px;
      }
      .intro {
        width: 512px;
        height: 90px;
        font-size: 16px;
        font-family: MicrosoftYaHei;
        font-weight: 400;
        color: rgba(255, 255, 255, 1);
        line-height: 30px;
        opacity: 0.8;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 3;
        -webkit-box-orient: vertical;
        margin-bottom: 124px;
      }
      .button {
        display: flex;
        align-items: center;
        .open {
          cursor: pointer;
        }
        .collection {
          margin-left: 10px;
        }
      }
    }
    .left-box {
      width: 40%;
    }
  }
  .fail {
    width: 1200px;
    margin: 0 auto;
    .fail-box {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
  }
}

.logo-bottom {
  font-size: 110px;
  margin-top: 20px;
  margin-bottom: 28px;
}
</style>